﻿/*******************文件共享指令**********************/
filepreview.directive('fileshare', ['$document', 'RequestTrainService', 'sysUserInfo', 'Msg', 'MsgCon', 'RequestPopService', 'FileUploader', '$timeout', function ($document, RTS, sysUserInfo, Msg, MsgCon, RPS, FileUploader, $timeout) {
    return {
        scope: {
            isdata: '=isdata'
        },
        templateUrl: "/system/directive/train/fileshare.html",
        restrict: 'ECA',
        link: function ($scope, el, attr) {

            $scope.fileshare = [], $scope.orgnameshar = [], $scope.sharorgfile = [], $scope.selfileshare = [], $scope.newselfileshare = [];
            $scope.sososhare = false, $scope.sharenull = false;
            $scope.ShareOrgName = "";
            var upids = "";
            
            //监控页面是否已打开
            $scope.$watch('isdata.divhide', function (newValue, oldValue, scope) {
                if ($scope.isdata.divhide) {                    
                    $scope.selfileshare = $scope.isdata.data;
                    console.log($scope.isdata.data);
                    //查询目前操作文件包含的企业列表和对应的详细文件
                    if ($scope.isdata.data != "") {
                        for (var i = 0; i < $scope.isdata.data.length; i++) {
                            if (upids == "") {
                                upids += $scope.isdata.data[i]["upId"];
                            } else {
                                upids += "," + $scope.isdata.data[i]["upId"];
                            }
                        }
                        $scope.getfilesharing(upids);
                    }
                } else {
                    $scope.isdata.filestyle = { "left": "0px" };
                    upids = "";
                }
            }, true);


            //查询文件共享情况
            $scope.getfilesharing = function (fid) {

                RTS.FileSharing({ fileids: fid, pageIndex: 1, pageSize: 10, userid: sysUserInfo.getuserinfo().user_ID, orgid: sysUserInfo.getuserinfo().organization_ID, powerLV: sysUserInfo.getuserinfo().powerLV }).then(function (kbdata) {
                    $scope.isdata.filestyle = { "left": (document.documentElement.clientWidth - 530) / 2 + "px" };
                    if (kbdata.errorcode == 0) {
                        if (kbdata.datas.length > 0) {
                            $scope.fileshare = kbdata.datas; //文件共享对应的企业

                            angular.forEach($scope.fileshare, function (item, index) {
                                $scope.newselfileshare = angular.copy($scope.selfileshare);

                                angular.forEach($scope.newselfileshare, function (item2, index2) {
                                    var abc = $scope.selfile(item.upid, item2.upId);
                                    if (abc) {

                                    } else {
                                        $scope.newselfileshare.splice(index2, 1);
                                    }
                                });
                                $scope.fileshare[index].selfileshare = angular.copy($scope.newselfileshare);
                                console.log($scope.fileshare);
                            });
                        } else {
                            $scope.fileshare = [];
                        }
                    } else {
                        Msg.error("文件共享情况获取异常！");
                    }
                });
            }

            //共享查询企业名称
            $scope.getorgnameshar = function () {
                if ($scope.orgsharename == null || $scope.orgsharename == "") {
                    alert("企业名称不能为空"); return;
                }
                $scope.sososhare = true;
                RTS.GetOrgNameShar({ files: upids, searchText: $scope.orgsharename, userid: sysUserInfo.getuserinfo().user_ID, orgid: sysUserInfo.getuserinfo().organization_ID, powerLV: sysUserInfo.getuserinfo().powerLV }).then(function (kbdata) {
                    if (kbdata.errorcode == 0) {
                        if (kbdata.datas.length > 0) {
                            $scope.orgnameshar = kbdata.datas;
                            $scope.sharenull = false;
                        } else { //没有查询到对应的企业
                            $scope.orgnameshar = [];
                            $scope.sharenull = true;
                        }
                    } else {
                        Msg.error("查询企业名称获取异常！");
                    }
                });
            }

            //查询已经分配给企业的共享文件
            $scope.getsharorgfile = function (fid, orgname) {
                $scope.ShareOrgName = orgname;
                RTS.AlreaSharOrgfile({ powerLV: sysUserInfo.getuserinfo().powerLV, userid: sysUserInfo.getuserinfo().user_ID, organization_ID: fid }).then(function (kbdata) {
                    if (kbdata.errorcode == 0) {
                        if (kbdata.datas.length > 0) {
                            $scope.sharorgfile = kbdata.datas;
                        } else {
                            $scope.sharorgfile = [];
                        }
                    } else {
                        $scope.sharorgfile = [];
                        Msg.error("查询企业名称获取异常！");
                    }
                });
            }


            //选择文件共享给企业
            $scope.setsharorgfile = function (fid) {
                RTS.setSharfile({ powerLV: sysUserInfo.getuserinfo().powerLV, fileids: upids, userid: sysUserInfo.getuserinfo().user_ID, orgid: sysUserInfo.getuserinfo().organization_ID, organization_ID: fid.orgid }).then(function (kbdata) {
                    if (kbdata.errorcode == 0) {
                        //实时展现选择的企业和共享文件
                        if ($scope.fileshare.length == 0) {
                            $scope.fileshare.push({ orgName: fid.orgName, orgid: fid.orgid, selfileshare: $scope.selfileshare }); //企业信息
                        } else {
                            var orgbool = false;
                            //如果文件已存在判断是否是新添加的文件
                            angular.forEach($scope.fileshare, function (item, index) {
                                if (item.orgid == fid.orgid) {
                                    orgbool = true;
                                    //判断选择的文件是否已存
                                    angular.forEach($scope.selfileshare, function (item2, index2) {
                                        angular.forEach(item.selfileshare, function (item3, index3) {
                                            if (item3.upId == item2.upId) {
                                            } else {
                                                $scope.fileshare[index].selfileshare.push(item2);
                                            }
                                        });
                                    });
                                }
                            });

                            if (!orgbool) {
                                $scope.fileshare.push({ orgName: fid.orgName, orgid: fid.orgid, selfileshare: $scope.selfileshare }); //企业信息
                            }
                        }
                        fid.share = 1;
                        Msg.success("选择文件共享给企业成功！");
                    } else {
                        Msg.error("选择文件共享给企业获取异常！");
                        $scope.extend.newtag = [];
                    }
                });
            }


            //删除企业共享文件
            $scope.delsharfile = function (fid, oporgid, filetype, deltype, optype) {
                RTS.delSharfile({ powerLV: sysUserInfo.getuserinfo().powerLV, fileids: fid, userid: sysUserInfo.getuserinfo().user_ID, orgid: sysUserInfo.getuserinfo().organization_ID, organization_ID: oporgid, fileType: filetype, deltype: deltype }).then(function (kbdata) {
                    if (kbdata.errorcode == 0) {
                        $scope.removeArray(fid, optype);
                        Msg.success("删除企业共享文件成功！");
                    } else {
                        Msg.error("删除企业共享文件异常！");
                    }
                });
            }

            //移除数组元素
            $scope.removeArray = function (upid, optype) {
                if (optype == 0) {
                    angular.forEach($scope.fileshare, function (item, index) {
                        //遍历判断是否已选 
                        if ($scope.fileshare[index].fileName == upid || $scope.fileshare[index].upid == upid) {
                            $scope.fileshare.splice(index, 1);
                        }
                    });
                } else if (optype == 1) {
                    angular.forEach($scope.sharorgfile, function (item, index) {
                        //遍历判断是否已选 sharorgfile
                        if ($scope.sharorgfile[index].upid == upid) {
                            $scope.sharorgfile.splice(index, 1);
                        }
                    });
                }
            }


            // 打开企业共享文件详细
            $scope.openshare = function (orgid, orgname) {
                $scope.isshare = true;
                $scope.isdata.filestyle = { "left": (document.documentElement.clientWidth - 1075) / 2 + "px" };
                $scope.getsharorgfile(orgid, orgname);
            }

            // 关闭企业共享文件详细
            $scope.closeshare = function () {
                $scope.isshare = false;
                $scope.isdata.filestyle = { "left": (document.documentElement.clientWidth - 530) / 2 + "px" };
            }


            // 关闭选中文件共享情况页面
            $scope.ishidefun = function () {
                $scope.isdata.divhide = false;
                $scope.sososhare = false;
                $scope.orgsharename = "";
                $scope.orgnameshar = [];
                $scope.closeshare();
            }
            //删除企业共享文件（已选择的共享文件）
            $scope.delfile = function (upId, upOrgId) {
                MsgCon.delnoicon().then(function (flag) {
                    if(flag)
                    $scope.delsharfile(upId, upOrgId, 0, 0, 0);
                });
            }

            //删除企业共享文件（全部共享文件）
            $scope.delfile2 = function (upId, upOrgId) {
                MsgCon.delnoicon().then(function (flag) {
                    $scope.delsharfile(upId, upOrgId, 0, 0, 1);
                });
            }





            $scope.selfile = function (upId, upIds) {
                if (upId != undefined && upIds != undefined) {
                    var indexNum = upId.indexOf(upIds);
                    if (indexNum >= 0) return true; else return false;
                } else {
                    return false;
                }
            }


            // 隐藏
            function documentKeydown(event) {
                if (event.keyCode === 27) {
                    $scope.$apply(function () {
                        $scope.ishidefun();
                    });
                }
            }

            $document.on('keydown', documentKeydown);
            $scope.$on('$destroy', function () {
                $document.off('keydown', documentKeydown);
            });

            var time;
            /********下载、删除、设置主版本对应的图标的提示文字显示和隐藏****************/
            $scope.showfileMsg = function (biem, filetype) {
                timeo = $timeout(function () {
                    switch (filetype) {
                        case 1: biem.fileattrplay = true; break;
                        case 2: biem.fileattrdel = true; break;
                        case 3: biem.fileattrvsion = true; break;
                        default: break;
                    }
                }, 500);
            }

            $scope.hidefileMsg = function (biem, filetype) {
                $timeout.cancel(timeo);
                switch (filetype) {
                    case 1: biem.fileattrplay = false; break;
                    case 2: biem.fileattrdel = false; break;
                    case 3: biem.fileattrvsion = false; break;
                    default: break;
                }
            }


        }
    };
} ]);